﻿
var async = require( 'async' );
var vacationImpl = require( '../Business/VacationImpl' );
var commonImpl = require( '../Business/CommonImpl' );
var util = require( '../Utils/utility' );
var _ = require( 'underscore' );

var that = this;
exports.listPage = function ( req, res, action, getListCall ) {

    var CurrentUser = req.session.CurrentUser,
        Menu = req.session.Menu,
        subMenu = req.session.SubMenu;

    if ( !!CurrentUser && !!Menu ) {

        async.parallel( {

            GetLeaveTypeList: function ( callback ) {

                commonImpl.GetLeaveTypeList( function ( list ) {
                    callback( null, list );
                });
            },

            GetStatusList: function ( callback ) {

                //var statusList = Json.stringify( util.StatusConst );
                callback( null, util.StatusConst );
            },

            GetMyVacationList: function ( callback ) {

                getListCall( function ( rows ) {
                    callback( null, rows );
                });


            }

        }, function ( err, results ) {

                results.GetLeaveTypeList.unshift( { Id: -1, LeaveType: "全部" });
                results.GetStatusList.unshift( { Key: "ALL", value: -1, Name: "全部" });

                results.GetMyVacationList.forEach( function ( item, i ) {

                    var startTime = item.LeaveBeginTime,
                        endTime = item.LeaveEndTime;

                    if ( startTime != null && startTime instanceof Date ) {
                        item.LeaveBeginTime = startTime.formate( "yyyy-MM-dd" );
                    }

                    if ( endTime != null && endTime instanceof Date ) {
                        item.LeaveEndTime = endTime.formate( "yyyy-MM-dd" );
                    }

                    var status = item.AppStatus;
                    if ( status != null ) {

                        item.AppStatus = _.find( util.StatusConst, function ( _stu ) {
                            return _stu.value == status;
                        })["Name"];
                    }

                });
                console.log(results.GetStatusList);
                res.render( 'vacation_list', {
                    title: action == "MyWork" ? "我的待办" : "休假申请",
                    Menus: Menu,
                    hasSubMenu: subMenu.length > 0,
                    SubMenus: subMenu,
                    LeaveTypes: results.GetLeaveTypeList,
                    StatusList: results.GetStatusList,
                    myVacationApplication: results.GetMyVacationList,
                    isPending: action == "MyWork"
                });
            });

    }
};
/*
 * GET Index
 */
exports.index = function ( req, res ) {

    var CurrentUser = req.session.CurrentUser;

    that.listPage( req, res, "List", function ( callback ) {

        vacationImpl.GetMyVacationList( CurrentUser.EmployeeNo, function ( rows ) {

            callback( rows );

        });
    });
}

/*
 * GET Detail
 */
exports.detail = function ( req, res ) {

    var user = req.session.CurrentUser,
        id = req.query.Id,
        ope = req.query.ope,
        Node = req.query.Node,
        vacationDetail = null;

    async.parallel( {

        GetLeaveTypeList: function ( callback ) {

            commonImpl.GetLeaveTypeList( function ( list ) {
                callback( null, list );
            });
        },

        GetVacationDetail: function ( callback ) {

            if( id ) {
               
                vacationImpl.GetVacationById( id, function( detail ){
                    vacationDetail = detail;
                    callback( null, detail);

                });
                 
            }else{
                callback( null, null );
            }
        }
    }, function ( err, results ) {

            if(vacationDetail){

                var startTime = vacationDetail.LeaveBeginTime,
                    endTime = vacationDetail.LeaveEndTime;

                if ( startTime != null && startTime instanceof Date ) {
                    vacationDetail.LeaveBeginTime = startTime.formate( "yyyy-MM-dd" );
                }

                if ( endTime != null && endTime instanceof Date ) {
                    vacationDetail.LeaveEndTime = endTime.formate( "yyyy-MM-dd" );
                }
            }

            res.render( 'vacation_detail', {
                title: "休假申请单",
                user: user,
                LeaveTypes: results.GetLeaveTypeList,
                detail: vacationDetail,
                isApprove : ope == "Approve",
                Node : Node
            });

        });

}

/*
 * POST Detail
 */
exports.operate = function ( req, res ) {

    var parameters = req.body,
        user = req.session.CurrentUser,
        operateResult = false;

        console.log(parameters.operate);

    async.series(
        {
            operate: function ( callback ) {
                switch ( parameters.operate ) {

                    case "Save":
                        break;
                    case "Submit":
                        vacationImpl.Submit( parameters, function ( err, result ) {
                            operateResult = result;
                            callback( null, operateResult );
                        });
                        break;
                    case "Approve":
                        vacationImpl.Approve( 
                            {
                                ApplicationID: parameters.ApplicationID,
                                OperatorID: user.Id,
                                ActionUserName: user.ChineseName,
                                CreatorID: parameters.CreatorID,
                                Node: parameters.Node
                            }, 
                            function( err, result ){
                                operateResult = result;
                                callback( null, operateResult );
                            });
                        break;
                    case "Reject":
                        vacationImpl.Reject( 
                            {
                                ApplicationID: parameters.ApplicationID,
                                OperatorID: user.Id,
                                ActionUserName: user.ChineseName,
                                CreatorID: parameters.CreatorID,
                                Node: parameters.Node
                            }, 
                            function( err, result ){
                                operateResult = result;
                                callback( null, operateResult );
                            });
                        break;
                }
            }

        }, function ( err, results ) {
            if ( operateResult == true ) {
                res.send( true );
            }
        });

}

/*
 * GET 我的待办
 */
exports.mywork = function ( req, res ) {

    var CurrentUser = req.session.CurrentUser;

    that.listPage( req, res, "MyWork", function ( callback ) {

        vacationImpl.GetMyWorkList( CurrentUser.Id, function ( rows ) {

            callback( rows );
        });
    });


}